//
//  MAHeartbeatManager.h
//  MiniAppSDK
//
//  Created by JiangHuifu on 2019/12/3.
//

#import <Foundation/Foundation.h>


typedef void (^ MAHeartbeatCallback)(NSDictionary * _Nullable result, NSError * _Nullable error);

typedef NS_ENUM(int32_t, MAHeartbeatRequestFactType) {
    MAHeartbeatRequestFactType_Start = 11,//心跳起始
    MAHeartbeatRequestFactType_Mid = 12,//中间心跳
    MAHeartbeatRequestFactType_End = 13,//心跳结束
};

typedef NS_ENUM(int32_t, MAGuardInstructionType) {
    MAGuardInstructionType_Undefined   = 0, // 未定义
    MAGuardInstructionType_Tips    = 1, //弹提示
    MAGuardInstructionType_Logout  = 2, // 强制下线
    MAGuardInstructionType_OpenUrl = 3, // 打开网页窗口
    MAGuardInstructionType_UserDefined = 4, // 用户自定义，data传json内容
    MAGuardInstructionType_Income  = 5, // 收益，不弹提示， data和ratio中传入比例，参考例1.2
    MAGuardInstructionType_Income_Tips = 6, // 收益且弹提示， data和ratio中传入比例，参考例1.2
    MAGuardInstructionType_Login = 12054, // 实名认证的指令
};

NS_ASSUME_NONNULL_BEGIN
@class MABaseApplication;

@interface ButtonTitleWithAction : NSObject

@property(nonatomic, strong, nullable) NSString *title;
@property(nonatomic, strong, nullable) void (^action)(BOOL checkBoxSelected);

- (instancetype)initWithTitle:(NSString *)title
                    andAction:(void (^)(BOOL checkBoxSelected))action;

@end

@interface MAHeartbeatManager : NSObject
@property(nonatomic,assign) BOOL isStarted;//是否已经启动了
@property(nonatomic,assign) BOOL isStopped;//是否已经结束了
@property(nonatomic,assign) BOOL isSuccessForStarted;//是否启动成功了
@property(nonatomic,weak) MABaseApplication* app;//寄主app（必须用weak，因为心跳控制器会被寄主app所持有）

@property(nonatomic,assign) NSInteger duration;//当前心跳间隔
@property(nonatomic,assign) CFAbsoluteTime prePulseTime;//上一次心跳时间
@property(nonatomic,assign) CFAbsoluteTime firstPulseTime;//第一次心跳脉冲时间

/**
 以app进行初始化
 */
- (instancetype)initWithApp:(MABaseApplication*)app;

/**
 一次心跳脉冲，供子类调用
 */
- (void)heartbeatPulse; //一次心跳脉冲

/**
 启动心跳
 一个心跳Manager只能启动一次，如果需要重新启动，则需要调用resetHeartbeatWithApp
 */
- (void)startHeartbeat;

/**
 关闭心跳
 一个心跳在启动之后可以且只可以关闭一次，关闭之后心跳Manager直接失效，重启需要调用resetHeartbeatWithApp
 */
- (void)endHeartbeat;

/**
 重置心跳Manager
 实质是关闭并去掉当前心跳
 然后重新生成一个新的心跳Manager
 */
+ (MAHeartbeatManager*)resetHeartbeatWithApp:(MABaseApplication*)app;

/**
 每次心跳脉冲请求数据回来后的指令动作
 factType: 当前心跳脉冲的状态 0: 未定义  11: 开始  12: 中间心跳  13: 结束
 params:请求回来的数据
 */
- (void)eventInHeartbeat:(void(^)(MAHeartbeatManager* manager))complete
                factType:(MAHeartbeatRequestFactType)factType
                  params:(NSDictionary*)params;


/**
 心跳脉冲请求
 */
- (BOOL)guardJudgeTimingReqWithFactType:(MAHeartbeatRequestFactType)factType
                               duration:(int32_t)duration
                              totalTime:(int32_t)totalTime
                           afterCertify:(int32_t)afterCertify
                      completionHandler:(MAHeartbeatCallback _Nullable)completionHandler;

@end

NS_ASSUME_NONNULL_END
